#include "CampusGraph.h"
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <windows.h>

using namespace std;

void showMenu() {
  cout << "\n===========================================\n";
  cout << "    华南师范大学南海校区导游系统\n";
  cout << "===========================================\n";
  cout << "1. 查询两景点间最短路径\n";
  cout << "2. 显示完整游览路线\n";
  cout << "3. 退出系统\n";
  cout << "===========================================\n";
  cout << "请选择功能 (1-3): ";
}

void clearScreen() { 
  system("cls"); 
}

void pauseScreen() {
  cout << "\n按任意键继续..." << flush;
  system("pause > nul");
}

int main() {
  // 设置控制台编码为UTF-8
  SetConsoleOutputCP(65001);
  SetConsoleCP(65001);

  clearScreen();
  cout << "\n欢迎使用华南师范大学南海校区导游系统！\n";
  cout << "正在加载地图数据...\n";

  CampusGraph graph;
  if (!graph.loadFromFile("campus_map.txt")) {
    cout << "错误：加载地图数据失败！\n";
    cout << "请确保campus_map.txt文件存在且格式正确。\n";
    pauseScreen();
    return 1;
  }

  cout << "地图数据加载成功！\n";
  graph.showVertices();
  pauseScreen();

  int choice;
  while (true) {
    clearScreen();
    showMenu();
    cin >> choice;
    if (cin.bad() || cin.fail()) {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      cout << "\n错误：无效的输入，请重新输入！\n";
      pauseScreen();
      continue;
    }

    if (choice == 3)
      break;

    switch (choice) {
    case 1: {
      clearScreen();
      string start, end;
      cout << "\n=== 最短路径查询 ===\n";
      graph.showVertices();
      cout << "\n请输入起点景点编号: ";
      cin >> start;
      cout << "请输入终点景点编号: ";
      cin >> end;
      cout << "从 " << graph.getVertexName(stoi(start)) << " 到 "
           << graph.getVertexName(stoi(end)) << " 的最短路径查询结果如下: ";
      auto result = graph.findShortestPath(start, end);
      cout << "\n查询结果：\n";
      cout << "----------------------------------------\n";
      if (result.second == -1) {
        cout << "抱歉，两个景点之间不可达或景点名称输入有误！\n";
        cout << "请检查景点名称是否正确。\n";
      } else {
        cout << "最短路径长度：" << result.second << " 米\n";
        cout << "推荐路线：";
        graph.showPath(result);
        cout << "\n";
      }
      cout << "----------------------------------------\n";
      pauseScreen();
      break;
    }
    case 2: {
      clearScreen();
      cout << "\n=== 完整游览路线推荐 ===\n";
      cout << "请输入寻找前k条最短路径的k值：";
      int k;
      cin >> k;
      cout << "\n正在寻找游览路线，请稍候...\n";
      auto paths = graph.findTourPaths("正门", k);

      if (paths.empty()) {
        cout << "\n抱歉，未能找到有效的游览路线。\n";
      } else {
        cout << "\n为您找到 " << paths.size() << " 条完整游览路线：\n";
        cout << "----------------------------------------\n";
        for (size_t i = 0; i < paths.size(); ++i) {
          cout << "路线 " << i + 1 << ": ";
          graph.showPath(paths[i]);
          cout << endl;
          cout << endl;
        }
        cout << "----------------------------------------\n";
      }
      pauseScreen();
      break;
    }
    default:
      cout << "\n错误：无效的选择，请输入1-3之间的数字！\n";
      pauseScreen();
    }
  }

  clearScreen();
  cout << "\n感谢使用华南师范大学南海校区导游系统！\n";
  cout << "祝您游览愉快！\n";
  pauseScreen();
  return 0;
}
